package com.guigu.ssyx.search.receiver;

import com.guigu.constant.MqConst;
import com.guigu.ssyx.search.service.SkuService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class SkuReceiver {
    @Autowired
    private SkuService skuService;

    /**
     * 商品上架
     * @param skuId
     * @param message
     * @param channel
     * @throws IOException
     * */

    @RabbitListener(bindings = @QueueBinding(
            value =@Queue(value = MqConst.QUEUE_GOODS_UPPER, durable = "true"), //队列
            exchange = @Exchange(value = MqConst.EXCHANGE_GOODS_DIRECT), //交换机
            key ={MqConst.ROUTING_GOODS_UPPER} //路由键
    ))
    public void upperSku(Long skuId, Message message, Channel channel)  {
        if(null != skuId){
            skuService.upperSku(skuId);
        }
        /**
         * 第一个参数：表示收到的消息的标号
         * 第二个参数：如果为true表示可以签收多个消息
         */
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 商品下架
     * @param skuId
     * @param message
     * @param channel
     * @throws IOException
     * */
    @RabbitListener(bindings = @QueueBinding(
            value =@Queue(value = MqConst.QUEUE_GOODS_LOWER, durable = "true"), //队列
            exchange = @Exchange(value = MqConst.EXCHANGE_GOODS_DIRECT), //交换机
            key ={MqConst.ROUTING_GOODS_LOWER} //路由键
    ))
    public void lowerSku(Long skuId, Message message, Channel channel)  {
        if(null != skuId){
            skuService.lowerSku(skuId);
        }
        /**
         * 第一个参数：表示收到的消息的标号
         * 第二个参数：如果为true表示可以签收多个消息
         */
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
